package com.deer.wms.ASN.manage.web;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.constant.AsnManageConstant;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.model.pack.PackBillBO;
import com.deer.wms.ASN.manage.service.AcceptRecordService;
import com.deer.wms.ASN.manage.service.PackBillService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.ResponseUtil;
import com.deer.wms.project.root.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : wei
 * @since : 2021-10-27 16:27:10
 **/
@Api(description = "组托拆托接口")
@RestController
@RequestMapping("/pack")
public class PackBillController {

    @Autowired
    private PackBillService packBillService;
    @Autowired
    private AcceptRecordService acceptRecordService;
    @Autowired
    private InventoryService inventoryService;


    @ApiOperation(value = "收货库存拆分")
    @PostMapping("/split")
    public Result split(@RequestBody PackBillBO packBillBO, @ApiIgnore @User CurrentUser currentUser) {
        //根据收货单找到库存
        AcceptRecord acceptRecord = acceptRecordService.getOneByField("accept_record_code", packBillBO.getAcceptRecordCode());

        //校验
        double unQty = acceptRecord.getQualifiedQuantity() - acceptRecord.getPackedQuantity();
        if (unQty < packBillBO.getPackQty() * packBillBO.getNumber()) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "拆分数量大于可拆分数量，请确认是否已拆分或未质检");
        }
        Inventory inventory = inventoryService.getById(acceptRecord.getInventoryId());
        if (ObjectUtil.isNull(inventory)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "不存在可拆分库存");
        }

        //核心方法，拆分库存
        List<Inventory> inventories = packBillService.pack(packBillBO, inventory, currentUser);
        return ResultGenerator.genSuccessResult(inventories);
    }

    @ApiOperation(value = "收货库存拆分")
    @PostMapping("/splitCt")
    public Result splitCt(@RequestBody PackBillBO packBillBO, @ApiIgnore @User CurrentUser currentUser) {
        //找到库存
        Inventory inv = inventoryService.getById(packBillBO.getInvId());
        //校验
        //packQty  新的数量
        if (inv.getQuantity() <= packBillBO.getPackQty()) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "拆分数量大于可拆分数量");
        }
        if(StringUtils.isEmpty(inv.getBoxCode())){
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "不允许对物料进行直接拆分");
        }

        //核心方法，拆分库存
        List<Inventory> retList = new ArrayList<>();
        List<Inventory> inventories = packBillService.splitCt(packBillBO, inv, currentUser);
        retList.addAll(inventories);

        Inventory newInv = inventoryService.getById(packBillBO.getInvId());
        if (ObjectUtil.isNotNull(newInv)) {
            retList.add(newInv);
        }

        return ResultGenerator.genSuccessResult(retList);
    }


    @ApiOperation(value = "组装")
    @PostMapping("/combCt")
    public Result combCt(@RequestBody PackBillBO packBillBO, @ApiIgnore @User CurrentUser currentUser) {
        //原始库存
        Inventory srcInv = inventoryService.getById(packBillBO.getInvId());
        //校验  是否是同物料，同批次
        //packQty  新的数量
        List<Inventory> combInves = new ArrayList<>();
        for (String combBox : packBillBO.getCombBoxes()) {
            Inventory combInv = inventoryService.getOneByField("box_code", combBox);
            if (ObjectUtil.notEqual(srcInv.getItemCode(), combInv.getItemCode())) {
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "不是同物料，不允许组装");
            }
            if(ObjectUtil.notEqual(srcInv.getBatchName(), combInv.getBatchName())) {
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "不是同批次，不允许组装");
            }
            combInves.add(combInv);
        }

        //核心方法，拆分库存
        Inventory retInv = packBillService.combCt(srcInv, combInves, currentUser);

        return ResultGenerator.genSuccessResult(retInv);
    }
}
